package com.ruoyi.purchase.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.purchase.mapper.PurchaseReturnGoodsMapper;
import com.ruoyi.purchase.mapper.PurchaseReturnMapper;
import com.ruoyi.purchase.pojo.PurchaseOrderMaterial;
import com.ruoyi.purchase.pojo.PurchaseReturn;
import com.ruoyi.purchase.pojo.PurchaseReturnGoods;
import com.ruoyi.purchase.service.PurchaseReturnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 采购退货Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-25
 */
@Service
public class PurchaseReturnServiceImpl implements PurchaseReturnService
{
    @Autowired
    private PurchaseReturnMapper purchaseReturnMapper;

    @Autowired
    private PurchaseReturnGoodsMapper purchaseReturnGoodsMapper;

    /**
     * 查询采购退货
     *
     * @param id 采购退货主键
     * @return 采购退货
     */
    @Override
    public PurchaseReturn selectPurchaseReturnById(Integer id)
    {
        return purchaseReturnMapper.selectPurchaseReturnById(id);
    }

    /**
     * 查询采购退货列表
     *
     * @param purchaseReturn 采购退货
     * @return 采购退货
     */
    @Override
    public List<PurchaseReturn> selectPurchaseReturnList(PurchaseReturn purchaseReturn)
    {
        return purchaseReturnMapper.selectPurchaseReturnList(purchaseReturn);
    }

    /**
     * 新增采购退货
     *
     * @param purchaseReturn 采购退货
     * @return 结果
     */
    @Transactional
    @Override
    public void insertPurchaseReturn(PurchaseReturn purchaseReturn)
    {
        purchaseReturn.setCreateTime(DateUtils.getNowDate());
        purchaseReturnMapper.insertPurchaseReturn(purchaseReturn);
        for (PurchaseReturnGoods purchaseReturnGoods : purchaseReturn.getPurchaseReturnGoods()){
            purchaseReturnGoods.setPurchaseReturnId(purchaseReturn.getId());
            purchaseReturnGoodsMapper.insertPurchaseReturnGoods(purchaseReturnGoods);
        }
    }

    /**
     * 修改采购退货
     *
     * @param purchaseReturn 采购退货
     * @return 结果
     */
    @Override
    public int updatePurchaseReturn(PurchaseReturn purchaseReturn)
    {
        for (PurchaseReturnGoods purchaseReturnGoods : purchaseReturn.getPurchaseReturnGoods()){
            if (purchaseReturnGoods.getId() != null){
                purchaseReturnGoods.setPurchaseReturnId(purchaseReturn.getId());
                purchaseReturnGoodsMapper.updatePurchaseReturnGoods(purchaseReturnGoods);
            }else {
                purchaseReturnGoods.setPurchaseReturnId(purchaseReturn.getId());
                purchaseReturnGoodsMapper.insertPurchaseReturnGoods(purchaseReturnGoods);
            }
        }
        purchaseReturn.setUpdateTime(DateUtils.getNowDate());
        return purchaseReturnMapper.updatePurchaseReturn(purchaseReturn);
    }

    @Override
    public int updateStatus(PurchaseReturn purchaseReturn) {
        purchaseReturn.setUpdateTime(DateUtils.getNowDate());
        return purchaseReturnMapper.updatePurchaseReturn(purchaseReturn);
    }

    /**
     * 批量删除采购退货
     *
     * @param ids 需要删除的采购退货主键
     * @return 结果
     */
    @Override
    public int deletePurchaseReturnByIds(Integer[] ids)
    {
        return purchaseReturnMapper.deletePurchaseReturnByIds(ids);
    }

    /**
     * 删除采购退货信息
     *
     * @param id 采购退货主键
     * @return 结果
     */
    @Override
    public int deletePurchaseReturnById(Integer id)
    {
        return purchaseReturnMapper.deletePurchaseReturnById(id);
    }
}
